﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PJ.Business;
using System.Collections.ObjectModel;
using System.Collections;
using Microsoft.Practices.Composite.Events;
using PJ.Business.SalePurchase;

namespace PJ.Presentation.SalePurchase
{
    public abstract class ShoppingCartModel<T> : EntityViewModel, IShoppingCartModel
        where T : IShoppingCartItemModel, new()
    {
        private IShoppingCartItemService m_ItemsService;
        private Dictionary<string, T> m_ItemsMap = new Dictionary<string, T>();

        public IShoppingCartItemService ItemsService
        {
            get 
            {
                if (m_ItemsService == null)
                    throw new ArgumentNullException();
                return m_ItemsService; 
            }
            set
            {
                m_ItemsService = value;
            }
        } 


        public ShoppingCartModel(IView view, IEventAggregator aggregator, IShoppingCartService service, IShoppingCartItemService itemsService)
            : base(view, aggregator, service)
        {
            Items = new ObservableCollection<T>();
            Items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ItemsChanged);
        }

        private void ItemsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                if (e.NewItems != null)
                {
                    foreach (T cartItem in e.NewItems)
                        OnAddItem(cartItem);
                }
                else
                    throw new ArgumentNullException();
        }
        protected virtual void OnAddItem(T newItem)
        {
            if (newItem == null)
                throw new ArgumentNullException();
            if (newItem.Order == 0.0)
            {
                if (Items.Count > 0)
                    newItem.Order = (from i in Items select i.Order).Max() + 10;
                else
                    newItem.Order = 10;
            }
        }
        /// <summary>
        /// GetValues. Sets the businness object properties using the model property values
        /// </summary>
        /// <param name="businessObject">business object to update</param>
        public override IBusinessObject GetValues(IBusinessObject businessObject)
        {
            base.GetValues(businessObject);
            return businessObject;
        }
        /// <summary>
        /// SetValues. Sets the model properties using the businessobject property values
        /// </summary>
        /// <param name="businessObject"></param>
        public override void SetValues(IBusinessObject businessObject)
        {
            base.SetValues(businessObject);
        }

        public override string ID
        {
            set
            {
                if (ID != value)
                {
                    base.ID = value;
                    Items.Clear();                    
                }
            }
        }

        public void AddItem(IShoppingCartItem item)
        {
            T itemModel = new T();
            itemModel.SetValues(item);
            Items.Add(itemModel);
        }

        protected virtual void LoadShoppingCartItems()
        {
            //List<string> list = ItemsService.GetFragments(ID);
            //foreach (string item in list)
            //    AddItem(item);
        }
        protected virtual void SaveShoppingCartItems()
        {
            //List<IShoppingCartItem> list = new List<IShoppingCartItem>();
            //foreach (T item in Items)
            //    list.Add((IShoppingCartItem)item.GetValues(ItemsService.CreateNewItem()));
            //list = ItemsService.SetFragments(list);
            //Items.Clear();
            //foreach (IShoppingCartItem item in list)
            //    AddItem(item);
        }

        public ObservableCollection<T> Items { get; set; }

        protected void ChangePartner(string partnerId)
        {
            ID = partnerId;
        }

    }
}
